package com.changgou.goods.controller;

import com.changgou.framework.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.haha.goods.entity.Brand;
import com.changgou.framework.entity.PageResult;
import com.changgou.framework.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * @author Yang
 * @date 2020/11/5
 */
@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    private BrandService brandService;
    /***
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return Result.ok(brandList);
    }

    /***
     * 根据ID查询品牌数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        //根据ID查询
        Brand brand = brandService.getById(id);
        return Result.ok(brand);
    }

    /***
     * 新增品牌数据
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        boolean b = brandService.save(brand);
        return b?Result.ok(null):Result.error("新增失败");
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id,@RequestBody Brand brand){
        brand.setId(id);
        boolean b = brandService.updateById(brand);
        return b?Result.ok(null):Result.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean b = brandService.removeById(id);
        return b?Result.ok(null):Result.error("删除失败");
    }
    @GetMapping("/search")
    public Result search(Brand brand){
        List<Brand> brands = brandService.getByBrand(brand);
        return Result.ok(brands);
    }

    //分页的条件查询
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size,@RequestBody(required = false) Brand brand){
        PageResult pageResult = brandService.findPage(brand, page, size);
        return Result.ok(pageResult);
    }

    /***
     * 根据分类实现品牌列表查询
     * /brand/category/{id}  分类ID
     */
    @GetMapping(value = "/category/{id}")
    public Result findBrandByCategory(@PathVariable(value = "id")Integer categoryId){
        //调用Service查询品牌数据
        List<Brand> categoryList = brandService.findByCategory(categoryId);
        return Result.ok(categoryList);
    }

}
